﻿(function() {
  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
    __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  define(['jquery', 'use!underscore', 'use!backbone', 'namespace', 'collections/album', 'views/photo', 'views/addPhoto'], function($, _, Backbone, global, Album, PhotoView, AddView) {
    var AlbumView;
    AlbumView = (function(_super) {

      __extends(AlbumView, _super);

      function AlbumView() {
        this.showAlbum = __bind(this.showAlbum, this);
        return AlbumView.__super__.constructor.apply(this, arguments);
      }

      AlbumView.prototype.el = $('#album');

      AlbumView.prototype.initialize = function() {
        var _this = this;
        return Album.on('reset', function() {
          var last_choice;
          _this.collection = Album;
          if ($('#albums:empty').length > 0) {
            _.each(_.uniq(_this.collection.pluck('type')), function(album) {
              return $('<option />', {
                text: album,
                value: album
              }).appendTo('#albums');
            });
          }
          last_choice = localStorage.getItem('photo_album') || 'bike';
          $('#albums').get(0).selectedIndex = $('option', '#albums').index($('option[value=\'' + last_choice + '\']', '#albums'));
          return _this.initCallback && _this.initCallback.func(_this.initCallback.args);
        });
      };

      AlbumView.prototype.render = function(album) {
        if (this.collection) {
          this.showAlbum(album);
        } else {
          Album.fetch();
          this.initCallback = {
            func: this.showAlbum,
            args: album
          };
        }
        return this;
      };

      AlbumView.prototype.showAlbum = function(album) {
        var modelsToRender;
        album = album || localStorage.getItem('photo_album') || 'bike';
        modelsToRender = _.filter(this.collection.models, function(item) {
          return item.get('type') === album;
        });
        return _.each(modelsToRender, (function(photoModel) {
          var photo;
          photo = new PhotoView({
            model: photoModel
          });
          return this.$el.append(photo.render('show').el);
        }), this);
      };

      AlbumView.prototype.events = {
        'click #addPhoto': 'createPhoto',
        'change #albums': 'changeAlbum'
      };

      AlbumView.prototype.createPhoto = function(e) {
        var newView;
        e.preventDefault();
        if ($('#add.photo').length > 0) {
          return;
        }
        newView = new AddView();
        $('.photo:first').before(newView.render('edit').el);
        return global.app.router.navigate('Photo/Create');
      };

      AlbumView.prototype.changeAlbum = function(e) {
        this.$el.find('figure').remove();
        localStorage.setItem('photo_album', e.target.value);
        return this.render(e.target.value);
      };

      return AlbumView;

    })(Backbone.View);
    return new AlbumView();
  });

}).call(this);
